<?php
class ShareService extends Service {
	//所有设置的值
	var $opt	=	array();
	var $group	=	'';
	var $model;
	
	/**
     +----------------------------------------------------------
     * 架构函数
     +----------------------------------------------------------
     * @author melec制作
     * @access public
     +----------------------------------------------------------
     */
	public function __construct() {
		//1.判断是否安装、是否运行该服务，系统服务可以不做判断
		//2.服务初始化
		$this->init();
		$this->run();
	}

	//服务初始化
	public function init(){
		$this->model = D('share');
		if($this->group==''){
			$this->group     =   'share';
		}
	}

	//运行服务，系统服务自动运行
	public function run(){
		//获取本组所有配置
		//todo：缓存
		$options	=	D('Option')->where("appname='$this->group'")->findAll();
		foreach((array) $options as $v){
			$this->opt[$v['name']]	=	$v['value'];
		}
	}
	
	function mylove(){
		echo 'mylove is here.';
	}

	/**
    * add_share
    * 分享弹窗
    *
    * 点击分享列表页或者分享内容页里的分享铵钮弹出的窗口
    * @param void
    * @access public
    * @return void
    */	
	function addShare_check($aimId,$typeId) {
		$mid = $this->mid;
		$where = "toUid = '$mid' AND typeId = '$typeId' AND aimId = '$aimId' AND isDel=0";
		$test = $this->where($where)->field('id')->find();
		if($test){
			echo -1;
		}else {
			echo 1;
		}
	}
	
	function addShare() {
		$aimId = $_REQUEST['aimId'];
		$this->assign('aimId',$aimId);

		$blog = $this->blog->where("id='$aimId'")->field('title')->find();

		$this->assign('title',$blog['title']);
		$this->assign($blog);

		$this->display();
	}
	
	function doaddShare1() {
		$type['typeId'] = 5;
		$type['typeName'] = '日志';
		$type['alias'] = 'blog';

		$info = h($_REQUEST['info']);
		$aimId = intval($_REQUEST['aimId']);

		$field = 'name,uid,content,title';
		$data = $this->blog->where("id='$aimId'")->field($field)->find();
		//$data['title'] = h($_REQUEST['title']);
		$intro = str_replace( "&amp;nbsp;","",t($data['content']));
		$data['intro'] = getBlogShort($intro,120);

		$fids = $_REQUEST['fids'];

		$result = $this->api->share_addShare($type,$aimId,$data,$info,0,$fids);
		echo $result;
	}
	/**
    * delAajax
    * 删除分享
    *
    * @param $_GET['id']  分享ID
    * @access public
    * @return -2 没权限删除 -1 不存在或已删除 0 失败 1 成功
    */
	function delAajax(){
		$id = intval($_REQUEST['id']);
		$share = $this->model->find($id);

		if(empty($share)){
			echo -1;
			exit();
		}
		if($share['toUid']!=$this->mid){
			echo -2;
			exit();
		}

		if(getC('isDel')){
			$result = $this->model->delete($id);
		}else{
			$result = $this->model->where("id='$id'")->setField('isDel','1');
		}

		echo $result;
	}



	/**
    * doaddShare
    * 站内增加分享API
    *
    * @param $type,$aimId,$toUid,$info,$purview,$data
    * @access public
    * @return  0 失败 1 成功 -1 已经分享 -2 分类出错 -3 内容为空  -4 不能分享自己的东西 -10 描述超过100字
    */
	function doAddShare($type,$aimId,$data,$info,$purview=0,$fri_ids=null,$url=null) {		
		$info = h($info);
		$check = $this->_check($type,$info,$data);
        if($check!=1){
        	return $check;
        }        
		if(empty($info)){
			$info = '&nbsp;';
		}

		$mid		 =	$this->mid;
		$username	 =	getUserName($this->mid);	
        
		$typeId = $type['typeId'];
		if(!empty($aimId)){
			$map['aimId'] = $aimId;
			$test = $this->isForbid($mid,$typeId,$aimId);
			if($test!=1)  return $test;
		}elseif(isset($data['url'])){
			$data['url'] = h($data['url']);
			$map['url'] = $data['url'];
		}
		
        $data = $this->_dealData($data);
        		
		$map['title'] = $data['title'];		
		$map['typeId'] = $typeId;
		$map['toUid'] = $mid;
		$map['toUserName'] = $username;
		//fromUid 方便以后扩充而设置
		$map['fromUid'] = $mid;
		$map['fromUserName'] = $username;
		
		$map['info'] = $info;		
		$map['cTime'] = time();
		$map['purview'] = $purview;
		$map['data'] = serialize($data);
	
        $result = $this->add($map);
		if($result){
			//发送通知和动态信息
            $this->_sendMessage($type,$aimId,$mid,$username,$data,$info,$fri_ids,$url,$result);
			//更新分享统计
			$this->_updateCount($mid);
			//增加积分
			$this->_addScoure($mid,$data,$type);
			return $result;
		}else{
			return 0;
		}	
		
	}
	
	function isForbid($mid,$typeId,$aimId){
		$where = "toUid = '$mid' AND typeId = '$typeId' AND aimId = '$aimId' AND isDel=0";
		$test = $this->where($where)->field('id')->find();
		if($test){
			return -1;
		}else {
			return 1;
		}
	}
	
	function _updateCount($mid){
		//更新分享统计
		$spaceDao	 =	D("Space");
		$spaceDao->changeCount( 'share',$this->getShareNum($mid) );
	}
	
	function _sendMessage($type,$aimId,$mid,$username,$data,$info,$fri_ids,$url,$result){
		if(isset($data['content']))  unset($data['content']);
		$body_data = $data;
		if(!empty($aimId))  $body_data['aimId'] = $aimId;
		$body_data['typeId'] = $type['typeId'];
		$body_data['toUid'] = $mid;
		$body_data['toUserName'] = $username;
		$body_data['info'] = $info;

		$body_data['id'] = $result;
		$title_data['id'] =  $result;
		if($type['typeId']==10){
			$body_data['username'] = getUserName($data['uid']);
			$userface = getUserFace($data['uid']);
			$body_data['userface'] = str_replace(SITE_URL,'{WR}',$userface);
		}
		
		$appid = D('App')->getChoiceId('share');
		D('Feed')->publish("share_".$type['alias'],$title_data,$body_data,$appid);

		$notifyDao = D('Notify');
		$notifyDao->setAppId($appid);
		
		$title_data['type'] = $type['typeName'];
		
		if(empty($url)){
			$url = $this->_getURL($type['typeId'],$aimId,$data);
		}
		if(empty($url)){
			$url = SITE_URL.'/apps/share/index.php?s=/Index/content/id/'.$result;
		}
		if(!empty($fri_ids)){			
			$notifyDao->send($fri_ids,"share_notice",$title_data,$body_data,$url);
		}
		
		$uid = $this->_getUid($data,$type);
		if(!empty($uid)&&$uid!=$mid){
			$notifyDao->send($uid,"share_notice2",$title_data,$body_data,$url);
		}
		if ($type['typeId']==10){
			$uid = $aimId;
			$notifyDao->send($uid,"share_notice3",$title_data,$body_data,$url);			
		}
	}
	
	function _check($type,$info,$data){
        if(empty($data)){        	
        	return -3;
        }
        if(!empty($data['url'])){
        	$url = h($data['url']);
        	if(empty($url)||$url=='http://'){
        		return 0;
        	}
        }
        if(StrLenW($info)>100){
        	return -10;
        }		
		if(empty($type['typeId'])){
			return -2;
		}
		return 1;	
	}
	
	function _dealData($data){
		foreach ($data as $k=>$v){
			$data[$k] = stripcslashes($v);
			$data[$k] = str_replace(SITE_URL,'{WR}',$v);
		}
		return $data;
	}
	
	function _getURL($typeId,$aimId,$data){
		switch ($typeId){
			case 5:
				$url = '{WR}/apps/blog/index.php?s=/Index/show/id/'.$aimId.'/mid/'.$data['uid'];
				break;
			case 6:
				$url = '{WR}/apps/photo/index.php?s=/Index/album/id/'.$aimId.'/uid/'.$data['userId'];
				break;
			case 7:
				$url = '{WR}/apps/photo/index.php?s=/Index/photo/id/'.$aimId.'/aid/'.$data['albumId'].'/uid/'.$data['userId'].'/type/mAll';
				break;
			case 8:
				$url = '{WR}/apps/group/index.php?s=/Group/index/gid/'.$aimId;
				break;
			case 9:
				$url = '{WR}/apps/group/index.php?s=/Topic/topic/gid/'.$data['gid'].'/tid/'.$data['id'];
				break;
			case 10:
				$url = '{WR}/apps/share/index.php?s=/Index/content/id/'.$aimId;
				break;
			case 12:
				$url = $data['url'];
				break;
			case 13:
				$url = '{WR}/apps/vote/index.php?s=/Index/pollDetail/id/'.$aimId;
				break;
			default: 
			    $url='';
			    break; 				
		}
		
		return $url;		
	}
	
	function _addScoure($mid,$data,$type){
		setScore($mid,'add_share');//发起分享
		
        $uid = $this->_getUid($data,$type);
		if(!empty($uid)){
			setScore($uid,'shared');//被分享
		}		
	}
	
	function _getUid($data,$type){
		if(!empty($data['uid'])&&$type['typeId']!=10){
			$uid = $data['uid'];
		}elseif (!empty($data['userId'])){
			$uid = $data['userId'];
		}
		return $uid;
	}






	/* 后台管理相关方法 */

	//启动服务，未编码
	public function _start(){
		return true;
	}

	//停止服务，未编码
	public function _stop(){
		return true;
	}

	//卸载服务，未编码
	public function _install(){
		return true;
	}

	//卸载服务，未编码
	public function _uninstall(){
		return true;
	}
}
?>